package com.zcoj.controller.Admin.announcement;

import com.zcoj.core.model.ApiResponse;
import com.zcoj.core.model.PageWrap;
import com.zcoj.pojo.Dto.Announcement;
import com.zcoj.service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author chenbin
 * @version 1.0
 * @description: TODO
 * @date 2023/4/19 14:41
 */

@RequestMapping("/adminAnnouncement")
@RestController
@PreAuthorize("hasAnyAuthority('super_admin','admin')")
public class adminAnnouncementController {


    @Autowired
    private AnnouncementService announcementService;


    @PostMapping("/createAnnouncement")
    public ApiResponse createAnnouncement(@RequestBody Announcement announcement){
        Integer integer = announcementService.create(announcement);

        if(Objects.nonNull(integer)){
            return ApiResponse.success("创建公告成功",integer);
        }else {
            return ApiResponse.failed("创建公告失败");
        }
    }


    @PostMapping("/announcementList")
    public ApiResponse contestList(@RequestBody PageWrap<Announcement> pageWrap){
        return ApiResponse.success(announcementService.findPage(pageWrap));
    }




    @GetMapping("/delAnnouncement/{id}")
    public ApiResponse delAnnouncement(@PathVariable Integer id){
        Integer integer = announcementService.deleteById(id);
        if(integer!=0){
            return ApiResponse.success("删除成功",null);
        }else {
            return ApiResponse.failed("删除失败！！！");
        }
    }


    @PostMapping("/updateAnnouncement")
    public ApiResponse updateAnnouncement(@RequestBody Announcement announcement){
        Integer updateById = announcementService.updateById(announcement);
        if(updateById!=0){
            return ApiResponse.success("更新成功",null);
        }else {
            return ApiResponse.failed("更新失败！！！");
        }
    }

    @GetMapping("/getAnnouncementById/{id}")
    public ApiResponse getAnnouncementById(@PathVariable Integer id){
        return ApiResponse.success( announcementService.findById(id));
    }
}
